package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import DB.DB_connection_pool;
import algorithm.Dynamic_information_select;
import bean.Dynamic_information;
import bean.Dynamic_informationList;


public class Hot_informations extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Dynamic_information_select dynamic_information_select = new Dynamic_information_select();
		
		PrintWriter out = response.getWriter();
		/*
		 * 24小时的热门动态
		 */
		List<Dynamic_information>  dynamic_informations_24hours = dynamic_information_select.Get_present_information(Dynamic_informationList.getDynamic_informations(),Dynamic_information_select.Twenty_four_hours);
		/*
		 * 一周热门动态
		 */
		List<Dynamic_information>  dynamic_informations_week = dynamic_information_select.Get_present_information(Dynamic_informationList.getDynamic_informations(),Dynamic_information_select.A_Week);

		
			try {
				Connection conn = DB_connection_pool.data_pool.getConnection();
				
				for(Dynamic_information dynamic_information : dynamic_informations_24hours){
					String select_sql = "select * from user_data where user_account = '"+dynamic_information.getUser_account()+"'";
					PreparedStatement ps_select_user = conn.prepareStatement(select_sql);
					ResultSet rs_user = ps_select_user.executeQuery();
				
				if(rs_user.next()){
					dynamic_information.setUser_photo_url(rs_user.getString("user_photo_path"));
					dynamic_information.setUser_nickname(rs_user.getString("user_nickname"));
				}
				
				rs_user.close();
				ps_select_user.close();
				}
				
				for(Dynamic_information dynamic_information : dynamic_informations_week){
					String select_sql = "select * from user_data where user_account = '"+dynamic_information.getUser_account()+"'";

					PreparedStatement ps_select_user = conn.prepareStatement(select_sql);
					ResultSet rs_user = ps_select_user.executeQuery();
				
				if(rs_user.next()){
					dynamic_information.setUser_photo_url(rs_user.getString("user_photo_path"));
					dynamic_information.setUser_nickname(rs_user.getString("user_nickname"));
				}
				
				rs_user.close();
				ps_select_user.close();
				}
				
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		
		
		
		JSONObject jsonObj = new JSONObject();
		
		jsonObj.put("dynamic_informations_24hours", dynamic_informations_24hours);
		jsonObj.put("dynamic_informations_week", dynamic_informations_week);
		jsonObj.put("result", true);
		
		out.print(jsonObj.toString());
	}

}
